package com.byb.server.service.impl;


import com.byb.server.service.dto.Wc.NormalAccessTokenDTO;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Async
@Component
public class WcAccessTokenTask {
    private final Logger log = LoggerFactory.getLogger(WcAccessTokenTask.class);

    protected static  NormalAccessTokenDTO normalAccessTokenDTO=new NormalAccessTokenDTO();

    protected static Jsapi_ticket_vm jsapi_ticket_vm =new Jsapi_ticket_vm();

    //普通access_token 获取地址
    private static final String NOMAL_A_T_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxb2b467961e051c00&secret=37f8c18ba56c283f76746ca1bbe7f95c";
    //jsapi_ticket 获取地址
    private static final String JSAPI_TICKET="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=";

    @Scheduled(fixedRate = 2 * 60 * 60*1000)
    public void getAccessToken(){
        log.info("start get WeChat Access_token : {}", "WeChat access_token");
        Gson gson = new Gson();
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(NOMAL_A_T_URL).build();
        try {
            Response response =okHttpClient.newCall(request).execute();
            if(!response.isSuccessful()){
                log.error("request weChat access_token : {}",response.toString());
            }else{

                String rep = response.body().string();
                log.info("respones.body : {}",rep);
                normalAccessTokenDTO = gson.fromJson(rep, new TypeToken<NormalAccessTokenDTO>(){}.getType());
                log.info("get WeChat Access_token success! : {}", normalAccessTokenDTO.getAccess_token());
                getJsapi_ticket(normalAccessTokenDTO.getAccess_token());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void  getJsapi_ticket(String access_token){
        log.info("start get WeChat ticket : {}","WeChat ticket");
        Gson gson = new Gson();
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(JSAPI_TICKET+access_token+"&type=jsapi").build();
        try {
            Response responses =client.newCall(request).execute();
            if(!responses.isSuccessful()){
                log.error("request weChat access_token : {}",responses.toString());
            }else{

                String rep = responses.body().string();
                log.info("respones.body : {}",rep);
                jsapi_ticket_vm = gson.fromJson(rep, new TypeToken<Jsapi_ticket_vm>(){}.getType());
                log.info("get WeChat ticket success! : {}", jsapi_ticket_vm.getTicket());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static class AccessToken_errro_vm{
        private String errcode;
        private String errmsg;

        public String getErrcode() {
            return errcode;
        }

        public void setErrcode(String errcode) {
            this.errcode = errcode;
        }

        public String getErrmsg() {
            return errmsg;
        }

        public void setErrmsg(String errmsg) {
            this.errmsg = errmsg;
        }
    }

    static class Jsapi_ticket_vm{
        private String errcode;
        private String errmsg;
        private String ticket;
        private Long expires_in;

        public String getErrcode() {
            return errcode;
        }

        public void setErrcode(String errcode) {
            this.errcode = errcode;
        }

        public String getErrmsg() {
            return errmsg;
        }

        public void setErrmsg(String errmsg) {
            this.errmsg = errmsg;
        }

        public String getTicket() {
            return ticket;
        }

        public void setTicket(String ticket) {
            this.ticket = ticket;
        }

        public Long getExpires_in() {
            return expires_in;
        }

        public void setExpires_in(Long expires_in) {
            this.expires_in = expires_in;
        }
    }
}
